<?php

require_once('./modules/tag.inc');

class ManageTagsRenderer extends Renderer
{
	function pageLoad()
	{
		switch( $this->action )
		{
			case "add_tag":
				$this->addTag();
				break;
			case "del_tag":
				$this->delTag();
				break;
			case "edit_tag":
				$this->editTag();
				break;
			case "":
			case "show":
			default:
				break;
		}
	}
	
	function delTag()
	{
		$tag = new Tag();
		$tag->delete($_GET["tagId"]);
		die();
	}

	function addTag()
	{
		$tag = new Tag();
		$tagId = "".$tag->add($_POST["tag"]);
			
		die("{Tag:{tag:'".$_POST["tag"]."',id:".$tagId."}}");
	}

	function editTag()
	{
		$tag = new Tag();
		$tag->load($_POST["tagId"]);
		
		$tag->tag = $_POST["tag"];

		$tag->update();
				
		die("{Tag:{tag:'".$tag->tag."',id:".$tag->id."}}");
	}
	
	function isPermitted()
	{
		//echo "permission check feeds";
		if ( isset($this->user) && $this->user->type >= 1 )
			return true;
		else
			return false;
	}
	
	function renderContent()
	{
		global $content;
		
		$content .= "
		<script src='./script/popup.js'></script>
		<script>		
			function askDelete(tagId)
			{
				var tag = $('#tag'+tagId);
				_popup.show('Should I delete ' + tag.html()+'?',deleteTag,tagId);
			}
						
			function deleteTag(isYes, tagId)
			{
				if(isYes)
				{
					$.get(document.location.href.replace(/\?.*/ig,\"?pg=manage_tags&act=del_tag&tagId=\"+tagId));
					$('#row'+tagId).remove();
				}
				_popup.hide();
			}

			function addTag()
			{
				var sTag = $('#txtAddTag').val();
				var reqUrl = document.location.href.replace(/\?.*/ig,\"?pg=manage_tags&act=add_tag\"); 
				$.post(reqUrl, 
				{ tag:sTag},
				function(data)
					{
						$('#dvTags').append('<tr id=\'row'+data.Tag.id+'\/>');
						$('#row'+data.Tag.id).append('<td style=\'border:solid 1px #EEE\'><span id=\'tag'+data.Tag.id+'\' class=\'tag\'>'+data.Tag.tag+'</span></td>');
						$('#row'+data.Tag.id).append('<td style=\'border:solid 1px #EEE\'><img src=\'./images/pencil.png\' alt=\'edit\' style=\'padding:2px;cursor:pointer\' onclick=\'showEdit('+data.Tag.id+')\'></img></td>');
						$('#row'+data.Tag.id).append('<td style=\'border:solid 1px #EEE\'><img src=\'./images/delete.png\' alt=\'delete\' style=\'padding:2px;cursor:pointer\' onclick=\'askDelete('+data.Tag.id+')\'></img></td>');
					},
				'json');
				$('#txtAddTag').val('');
			}
			
			function showEdit(tagId)
			{
				$('#txtEditTag').val($('#tag'+tagId).html());
				$('#lblTagId').val(tagId);
				$('#dvEdit').show();
			}
			
			function cancelEdit()
			{
				$('#dvEdit').hide();
			}
			
			function editTag()
			{
				var sTag = $('#txtEditTag').val();
				var sTagId = $('#lblTagId').val();
				var reqUrl = document.location.href.replace(/\?.*/ig,\"?pg=manage_tags&act=edit_tag\"); 
				$.post(reqUrl, 
				{ tag:sTag,tagId:sTagId},
				function(data)
					{
						$('#row'+data.Tag.id).children().remove();
						$('#row'+data.Tag.id).append('<td style=\'border:solid 1px #EEE\'><span id=\'tag'+data.Tag.id+'\' class=\'tag\'>'+data.Tag.tag+'</span></td>');
						$('#row'+data.Tag.id).append('<td style=\'border:solid 1px #EEE\'><img src=\'./images/pencil.png\' alt=\'edit\' style=\'padding:2px;cursor:pointer\' onclick=\'showEdit('+data.Tag.id+')\'></img></td>');
						$('#row'+data.Tag.id).append('<td style=\'border:solid 1px #EEE\'><img src=\'./images/delete.png\' alt=\'delete\' style=\'padding:2px;cursor:pointer\' onclick=\'askDelete('+data.Tag.id+')\'></img></td>');
					},
				'json');
				$('#txtEditTag').val('');
				cancelEdit();
			}
		</script>";
		
		$content .= 
		"<p><h1>Manage tags</h1></p>
		<span id='popupHolder'/>
		<span id='dvEdit' style='position:absolute;background-color:#EEE;padding:5px;border:solid 1px #DDD;display:none;'>
			<input id='lblTagId' type='text' style='display:none;'/>
			<table>
				<tr>
					<td>Tag </td><td><input id='txtEditTag' type='text' len='100'/></td>
				</tr>
				<tr>
					<td>
						<input type='button' value='Save' onclick='editTag();'/>
						<input type='button' value='Cancel' onclick='cancelEdit();'/>
					</td>
				</tr>
			</table>
		</span>
		<p>
			<table>
				<tr>
					<td>Tag </td><td><input id='txtAddTag' type='text' len='100'/></td>
				</tr>
				<tr>
					<td>
						<input type='button' value='add' onclick='addTag();'/>
					</td>
				</tr>
			</table>
		</p>
		<table width='93%' id='dvTags' style='line-height:1.8em;whitespace:no-break;'>";

		$array = Tag::getAll();
		foreach($array as $row)
			$content .= "<tr id='row".$row['id']."'>
							<td style='border:solid 1px #EEE'>
								<span id='tag".$row['id']."' class='tag'>".$row['tag']."</span>
							</td>
							<td style='border:solid 1px #EEE'>
								<img src='./images/pencil.png' alt='edit' style='padding:2px;cursor:pointer' onclick='showEdit(".$row['id'].")'></img>
							</td>
							<td style='border:solid 1px #EEE'>
								<img src='./images/delete.png' alt='delete' style='padding:2px;cursor:pointer' onclick='askDelete(".$row['id'].")'></img>
							</td>";

		$content .=	"</table>";
	}
}

$renderer = new ManageTagsRenderer();

?>
